<template>
  <div class="ali-live-config gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.aliLiveConfig') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="180px"
      size="default"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        :label="$t('sysManagement.pushDomain')"
        style="width:640px"
        prop="pushDomain"
      >
        <el-input
          v-model="dataForm.pushDomain"
          :placeholder="$t('sysManagement.pushDomain')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.pullDomain')"
        style="width:640px"
        prop="pullDomain"
      >
        <el-input
          v-model="dataForm.pullDomain"
          :placeholder="$t('sysManagement.pullDomain')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.appName')"
        style="width:640px"
        prop="appName"
      >
        <el-input
          v-model="dataForm.appName"
          :placeholder="$t('sysManagement.appName')"
          controls-position="right"
          maxlength="50"
          show-word-limit
          @input="inputLimit"
        />
        <p class="live-tips">
          {{ $t("liveRoom.liveTip2") }}
        </p>
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.pushAuthKey')"
        style="width:640px"
        prop="pushAuthKey"
      >
        <el-input
          v-model="dataForm.pushAuthKey"
          :placeholder="$t('sysManagement.pushAuthKey')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.pullAuthKey')"
        style="width:640px"
        prop="pullAuthKey"
      >
        <el-input
          v-model="dataForm.pullAuthKey"
          :placeholder="$t('sysManagement.pullAuthKey')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.isHttps')"
        prop="isHttps"
      >
        <el-switch
          v-model="dataForm.isHttps"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>

      <div
        class="default-btn"
        @click="onSubmit()"
      >
        {{ $t('sysManagement.save') }}
      </div>
    </el-form>
  </div>
</template>

<script setup>

import { ElMessage } from 'element-plus'

const dataForm = ref({
  pushDomain: null,
  pullDomain: null,
  appName: null,
  pushAuthKey: null,
  pullAuthKey: null,
  isHttps: 0
})
const dataRule = {
  pushDomain: [
    { required: true, message: `${$t('sysManagement.pushDomain')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  pullDomain: [
    { required: true, message: `${$t('sysManagement.pullDomain')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  appName: [
    { required: true, message: `${$t('sysManagement.appName')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  pushAuthKey: [
    { required: true, message: `${$t('sysManagement.pushAuthKey')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  pullAuthKey: [
    { required: true, message: `${$t('sysManagement.pullAuthKey')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ]
}
onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'ALI_LIVE_CONFIG'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data) {
        dataForm.value = JSON.parse(data)
      }
    })
    dataFormRef.value?.resetFields()
  })
}
const inputLimit = (val) => {
  // 正则匹配非数字、大小写字母、短划线(-)、下划线(_)、等号(=)
  const reg = /[^A-Za-z0-9_=-]/
  // 没有则返回-1，有则返回对应位置
  const n = val.search(reg)
  if (n !== -1) {
    dataForm.value.appName = val.slice(0, n)
  }
}

const emit = defineEmits(['refreshDataList'])
const dataFormRef = ref(null)
// 表单提交
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/sys/pconfig/save'),
        method: 'post',
        data: http.adornData({
          paramKey: 'ALI_LIVE_CONFIG',
          paramValue: JSON.stringify(dataForm.value),
          remark: $t('sysManagement.aliLiveConfig')
        })
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            emit('refreshDataList')
          }
        })
      })
    }
  })
}
defineExpose({ init })
</script>
<style lang="scss" scoped>
.live-tips {
  color: #aaa;
  line-height: 20px;
  margin: 0;
  padding: 0;
}
</style>
